package com.neugh.controller;


import com.neugh.entity.Judge;
import com.neugh.service.CommService;
import com.neugh.service.JudgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sot
 * @since 2020-05-15
 */
@Controller
public class CommController {
    @Autowired
    JudgeService judgeService;

    @Autowired
    CommService commService;

    @RequestMapping("dongtaiTwo")
    public String dongtaiTwo(int commId, Model model){
        Judge comm = judgeService.selectCommById(commId);
        model.addAttribute("comm",judgeService.selectCommById(commId));
        System.out.println("------------------------------"+comm);
        model.addAttribute("JudgeList",judgeService.selectJudgeById(commId));
        return "dongtai2";
    }

    @RequestMapping("publish")
    public String fabu(){
        return "publish";
    }

    @RequestMapping("insertComm")
    public String insert(@RequestParam("commContent") String commContent, @RequestParam("commImg") MultipartFile commImg, HttpServletRequest request) throws IOException {

        String orignalFilename= commImg.getOriginalFilename(); //获取文件名
        //获取文件后缀
        String suffix=orignalFilename.substring(orignalFilename.lastIndexOf("."));
        //新的名字，防止重复存数据库的名字
        String newFilename="jvbao_"+ UUID.randomUUID().toString()+suffix;
        File file1=new File("D:\\ider\\cangku\\neughlinlibang\\Neugh\\itcy\\src\\main\\resources\\static\\img\\"+newFilename);

            commImg.transferTo(file1);
            boolean n =  commService.insertComm(commContent,newFilename);
            System.out.println(n);
            if(n){
                return "redirect:dong";
            }

        return "";
    }

}


